package com.deyou.activity;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.deyou.R;
import com.deyou.adapter.FacilitySectionAdapter;
import com.deyou.base.BaseActivity;
import com.deyou.callback.ServerApi;
import com.deyou.event.HouseFacilityEvent;
import com.deyou.model.FacilityAttrBean;
import com.deyou.model.FacilitySection;
import com.deyou.model.LzyResponse;
import com.deyou.util.AESUtil;
import com.deyou.util.Urls;
import com.google.gson.reflect.TypeToken;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONObject;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

import static com.deyou.R.id.viewpager;

public class HouseFacilityListActivity extends BaseActivity {

    @BindView(R.id.tv_mid_title)
    TextView tvMidTitle;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;

    private FacilitySectionAdapter mAdapter;
    private List<FacilitySection> list;
    private StringBuilder baseFacilitySb, easyFacilitySb, specialFacilitySb, otherFacilitySb;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hotel_facility_list);
        ButterKnife.bind(this);
        initView();
        getFacilities();
    }

    private void initView() {
        tvMidTitle.setText("配套设施");
        tvRight.setVisibility(View.VISIBLE);
        tvRight.setText("保存");
        tvRight.setTextColor(getResources().getColor(R.color.color_FF832D));
        recyclerView.setLayoutManager(new GridLayoutManager(this, 2));
    }

    private void initListener() {
        mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                FacilitySection section = list.get(position);
                if (null != section && null != section.t){
                    if (section.t.isSelected){
                        section.t.setSelected(false);
                    } else {
                        section.t.setSelected(true);
                    }
                }
            }
        });
    }

    private void getFacilities() {
        Type type = new TypeToken<LzyResponse<FacilityAttrBean>>() {}.getType();
        ServerApi.<LzyResponse<FacilityAttrBean>>getData(type, Urls.getFacilities, "", "")//
                .subscribeOn(Schedulers.io())//
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(@NonNull Disposable disposable) throws Exception {
                        showLoading("获取设施中...");
                    }
                })//
                .map(new Function<LzyResponse<FacilityAttrBean>, FacilityAttrBean>() {
                    @Override
                    public FacilityAttrBean apply(@NonNull LzyResponse<FacilityAttrBean> response) throws Exception {
                        return response.data;
                    }
                })//
                .observeOn(AndroidSchedulers.mainThread())  //
                .subscribe(new Observer<FacilityAttrBean>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        addDisposable(d);
                    }

                    @Override
                    public void onNext(@NonNull FacilityAttrBean facilityAttrBean) {
                        if (null != facilityAttrBean) {
                            list = new ArrayList<>();
                            if (null != facilityAttrBean && facilityAttrBean.commonList != null && facilityAttrBean.commonList.size() > 0) {
                                list.add(new FacilitySection(true, "最常见"));
                                for (FacilityAttrBean.Facility facility : facilityAttrBean.commonList) {
                                    if (null != facility) {
                                        facility.type = 1;
                                        list.add(new FacilitySection(facility));
                                    }
                                }
                            }
                            if (null != facilityAttrBean && facilityAttrBean.convenientList != null && facilityAttrBean.convenientList.size() > 0) {
                                list.add(new FacilitySection(true, "便利设施"));
                                for (FacilityAttrBean.Facility facility : facilityAttrBean.convenientList) {
                                    if (null != facility) {
                                        facility.type = 2;
                                        list.add(new FacilitySection(facility));
                                    }

                                }
                            }
                            if (null != facilityAttrBean && facilityAttrBean.specialList != null && facilityAttrBean.specialList.size() > 0) {
                                list.add(new FacilitySection(true, "特殊"));
                                for (FacilityAttrBean.Facility facility : facilityAttrBean.specialList) {
                                    if (null != facility) {
                                        facility.type = 3;
                                        list.add(new FacilitySection(facility));
                                    }
                                }
                            }
                            if (null != facilityAttrBean && facilityAttrBean.thoerList != null && facilityAttrBean.thoerList.size() > 0) {
                                list.add(new FacilitySection(true, "其他"));
                                for (FacilityAttrBean.Facility facility : facilityAttrBean.thoerList) {
                                    if (null != facility) {
                                        facility.type = 4;
                                        list.add(new FacilitySection(facility));
                                    }
                                }
                            }
                            mAdapter = new FacilitySectionAdapter(R.layout.facility_item_layout, R.layout.facility_head_item, list);
                            recyclerView.setAdapter(mAdapter);
                            initListener();
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        e.printStackTrace();            //请求失败
                    }

                    @Override
                    public void onComplete() {
                        dismissLoading();
                    }
                });
    }

    @OnClick({R.id.iv_left_back, R.id.tv_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_left_back:
                finish();
                break;
            case R.id.tv_right:
                splitFacilityString();
                EventBus.getDefault().post(new HouseFacilityEvent(baseFacilitySb.toString(), easyFacilitySb.toString(), specialFacilitySb.toString(), otherFacilitySb.toString()));
                finish();
                break;
        }
    }

    private void splitFacilityString() {
        baseFacilitySb = new StringBuilder();
        easyFacilitySb = new StringBuilder();
        specialFacilitySb = new StringBuilder();
        otherFacilitySb = new StringBuilder();

        for (FacilitySection section : mAdapter.getData()){
            if (null != section && !section.isHeader){
                if (null != section.t && section.t.isSelected){
                    switch (section.t.type){
                        case 1:
                            baseFacilitySb.append(section.t.id).append(",");
                            break;
                        case 2:
                            easyFacilitySb.append(section.t.id).append(",");
                            break;
                        case 3:
                            specialFacilitySb.append(section.t.id).append(",");
                            break;
                        case 4:
                            otherFacilitySb.append(section.t.id).append(",");
                            break;
                    }
                }
            }
        }
    }
}
